import { defineStore } from 'pinia'
// @ts-ignore
// import {ElLoading} from "element-plus";
import {ElMessage} from 'element-plus'
import {getCurrentInstance} from "vue";
// @ts-ignore

export default defineStore('storeIdD', {
    // 其他配置...
    state: ()=>{
        return{
            list: [],
            searchListA: [],
            keyArr: []
        }
    },
    getters: {
    },
    // 同步异步统一到actions中了
    actions: {
        lock(){
            let a = 0
            this.keyArr.forEach((ID:any):void=>{
                this.list.forEach((item:any):void=>{
                    if(item.state === '正常'&&item.key === ID){
                        a++
                        item.state = '锁定'
                        ElMessage({
                            message: '锁定成功',
                            type: 'success',
                        })
                    }
                })
            })
            if(this.keyArr.length === 0){
                ElMessage({
                    message: '没有选定的选项，请选择需要锁定的选项',
                })
            }else if(a===0){
                ElMessage({
                    message: `已经是锁定状态，请选择别的选项`,
                    type: 'warning',
                })
                return
            }
        },
        Unlock() :void{
            let a = 0
            this.keyArr.forEach((ID:any):void=>{
                console.log(ID)
                this.list.forEach((item:any):void=>{
                    if(item.state === '锁定'&&item.key === ID){
                        a++
                        item.state = '正常'
                        ElMessage({
                            message: '解锁成功',
                            type: 'success',
                        })
                    }
                })
            })
            if(this.keyArr.length === 0){
                ElMessage({
                    message: '没有选定的选项，请选择需要锁定的选项',
                })
            }else if(a===0){
                ElMessage({
                    message: '已经是解锁状态，请选择别的选项',
                    type: 'warning',
                })
            }
        },
        keyArrA(obj:any):void{
            this.keyArr = obj
            console.log(this.keyArr)
        },
        // 添加
        add(obj: object){
            // @ts-ignore
            this.list.push(obj)
            ElMessage({
                type: 'success',
                message: '添加成功'
            })
        },
        async request() {
            let {proxy}: any = getCurrentInstance()
            let {data: {assemble}} = await proxy.$axios.get('/json/evaluate.json')
            this.list = assemble
            this.searchListA = this.list
        },
        editA(obj: any, key: number) {
            console.log(obj)
            this.list.some((item: any) => {
                if (item.key === key) {
                    item.amount = obj.amount
                    item.name = obj.name
                    item.number = obj.number
                    item.state = obj.state
                    item.picture = obj.picture
                    item.requion = obj.requion
                    item.time = obj.time
                    return true
                }
            })
            ElMessage({
                type: 'success',
                message: '修改成功'
            })
        },
        // 商品列表删除数据
        filterB(key: number): void {
            this.list = this.list.filter((item: any): boolean => item.key !== key)
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
        },
        // 搜索功能
        searchA(val:any){
            console.log(val)
            this.searchListA = this.list.filter((item:any)=>{
                if(val.state===''&&val.name===''&&val.time===''){
                    // return this.searchListA = this.list, this.radio = '全部订单',console.log(this.radio)
                    return this.searchListA = this.list
                }else{
                    console.log(item.state)
                    return item.state === val.state ||item.name === val.name || item.time ===val.time
                }
            })
            console.log(this.searchListA)
        }
    }
});

